<?php

/**
 * Description of Provider_user
 *
 * @author Administrator
 */
class Provider_user extends CI_Controller {

  protected $class;
  protected $model;

  public function __construct() {
    parent::__construct();
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $this->class = $this->router->fetch_class();
    $model_name = $this->class . "_model";
    $this->model = new $model_name();
    set_userdata('currentURL', current_url());
  }

  protected function _list_data() {
    return array();
  }

  public function index($max = '', $offset = '') {
    $offset = $offset + 0;
    $max = $this->setting_model->item('max_record');
    $total = $this->model->count();
    $this->load->helper('paging');
    $paging = paging('admin/' . $this->class . '/index', $total, $max, $offset);
    $this->load->view('admin/' . $this->class . '/list', array_merge($this->_list_data(), array(
                'objects' => $this->model->get_all(null, null, null, null, null, null, null, null, null, null, null, null, array('date_create' => 'desc'), $max, $offset),
                'total' => $total, 'offset' => $offset, 'max' => $max, 'paging' => $paging)));
  }

  public function show($id = '') {
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        $user = $this->user_model->get($obj['user_id']);
        if ($user) {
          unset($user['id']);
          $this->load->view('admin/' . $this->class . '/show', array('obj' => array_merge($user, $obj)));
          return;
        }
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/' . $this->class);
  }

  public function create() {
    $this->load->view('admin/' . $this->class . '/create');
  }

  public function save() {
    $user = $_REQUEST;
    $group = $this->group_model->get(array('name' => 'Provider'));
    if (!empty($group)) {
      $user['group_id'] = $group['id'];
    } else
      $user['group_id'] = 0;
    $user['id'] = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : null;
    $user = $this->user_model->create_obj($user);
    $provider = $this->model->create_obj($_REQUEST);
    $errors = array_merge($this->user_model->validate($user), $this->model->validate($provider));
    if (!empty($errors))
      set_flash_error($errors);
    else {
      $user['salt'] = $this->auth->generateActiveCode(10);
      $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($user['password']));
      if ($this->setting_model->item('enviroment') == 1) {
        $user['is_active'] = true;
      }
      $user = $this->user_model->save($user);
      if (is_integer($user)) {
        $avatar = $this->base_lib->_upload_img('avatar', $this->class);
        $provider['avatar'] = $avatar;
        $provider['user_id'] = $user;
        if ($this->setting_model->item('enviroment') == 1) {
          $provider['status'] = PROFILE_STATUS_ACTIVE;
        } else {
          $provider['status'] = PROFILE_STATUS_UNACTICE;
        }
        $provider_id = $this->model->save($provider);
        if (is_integer($provider_id)) {
          if ($this->setting_model->item('require_active_via_email')) {
            $this->load->library('provider_user_lib');
            $this->provider_user_lib->email_active_account(array_merge($user, $provider));
          }
          set_flash_message($this->lang->line('message.created', array($this->lang->line('label.' . $this->class), $provider_id)));
          redirect('admin/' . $this->class);
          return;
        } else {
          $this->user_model->delete($user);
          set_flash_error($this->lang->line('error.faltal.error'));
        }
      } else
        set_flash_error($this->lang->line('error.faltal.error'));
    }
    $this->load->view('admin/' . $this->class . '/create', array('obj' => $_REQUEST));
  }

  public function edit($id = '') {
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        $user = $this->user_model->get($obj['user_id']);
        if (!empty($user)) {
          unset($user['id']);
          $this->load->view('admin/' . $this->class . '/edit', array('obj' => array_merge($obj, $user)));
          return;
        }
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/' . $this->class);
  }

  public function update() {
    $user = $_REQUEST;
    $group = $this->group_model->get(array('name' => 'Provider'));
    if (!empty($group)) {
      $user['group_id'] = $group['id'];
    } else
      $user['group_id'] = 0;
    $user['id'] = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : null;
    $user = $this->user_model->create_obj($user);
    $provider = $this->model->create_obj($_REQUEST);
    $errors = array_merge($this->user_model->validate($user), $this->model->validate($provider));
    if (!empty($errors))
      set_flash_error($errors);
    else {
      if (!empty($_REQUEST['password'])) {
        $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($_REQUEST['password']));
      } else
        unset($user['password']);
      $user = $this->user_model->save($user);
      if ($user == true) {
        $avatar = $this->base_lib->_upload_img('avatar', $this->class);
        if (!empty($avatar)) {
          $provider['avatar'] = $avatar;
        }
        $id = $this->model->save($provider);
        if ($id === true) {
          set_flash_message($this->lang->line('message.updated', array($this->lang->line('label.' . $this->class), $_REQUEST['id'])));
          redirect('admin/' . $this->class);
          return;
        } else
          set_flash_error($this->lang->line('error.faltal.error'));
      } else
        set_flash_error($this->lang->line('error.faltal.error'));
    }
    $this->load->view('admin/' . $this->class . '/edit', array('obj' => $_REQUEST));
  }

  public function delete($id = '') {
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        if (!empty($obj['avatar'])) {
          @delete_files($this->setting_model->item('upload_path') . 'provider_user/' . $obj['avatar']);
        }
        $this->model->delete($id);
        $this->user_model->delete($obj['user_id']);
        set_flash_message($this->lang->line('message.deleted', array($this->lang->line('label.' . $this->class), $id)));
        redirect('admin/' . $this->class);
        return;
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/' . $this->class);
  }

  public function action() {
    if (!empty($_REQUEST['btnAction']) && !empty($_REQUEST['action_to'])) {
      $action_to = $_REQUEST['action_to'];
      $action_to = is_array($action_to) ? $action_to : array($action_to);
      if ($_REQUEST['btnAction'] == 'delete') {
        foreach ($action_to as $id) {
          $obj = $this->model->get($id);
          if (!empty($obj)) {
            @delete_files($this->setting_model->item('upload_path') . 'provider_user/' . $obj['avatar']);
            $this->model->delete(array('id' => $id));
            $this->user_model->delete($obj['user_id']);
          }
        }
      } else if ($_REQUEST['btnAction'] == 'active') {
        foreach ($action_to as $id) {
          $obj = $this->model->get($id);
          if (!empty($obj)) {
            $obj['status'] = PROFILE_STATUS_ACTIVE;
            $this->model->save($obj);
          }
        }
      } else if ($_REQUEST['btnAction'] == 'block') {
        foreach ($action_to as $id) {
          $obj = $this->model->get($id);
          if (!empty($obj)) {
            $obj['status'] = PROFILE_STATUS_BLOCK;
            $this->model->save($obj);
          }
        }
      }
      set_flash_message($this->lang->line('message.action.success'));
    } else
      set_flash_error($this->lang->line('error.action.no.select'));
    redirect('admin/' . $this->class);
  }

  public function search($max = '', $offset = '') {
    $offset = $offset + 0;
    $conditions = $_REQUEST;
    $in_list = array();
    $params = array();
    foreach ($conditions as $field => $value) {
      if ($value == 'all') {
        unset($conditions[$field]);
        continue;
      }
      if (is_array($value)) {
        foreach ($value as $v)
          $params[] = $field . '[]=' . $v;
        $in_list[$field] = $value;
        unset($conditions[$field]);
      } else
        $params[] = $field . '=' . $value;
    }
    $params = '?' . implode('&', $params);
    $total = $this->model->count($conditions, null, $in_list);
    $max = $this->setting_model->item('max_record');
    $this->load->helper('paging');
    $paging = paging('admin/' . $this->class . '/search', $total, $max, $offset, array(), $params);

    $this->load->view('admin/' . $this->class . '/list', array_merge($_REQUEST, $this->_list_data(), array(
                'objects' => $this->model->get_all($conditions, null, $in_list, null, null, null, null, null, null, null, null, null, null, $max, $offset),
                'total' => $total, 'offset' => $offset, 'max' => $max, 'paging' => $paging)));
  }

}